package com.smart.car.gateway.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;

/**
 * TODO swagger网关过滤器，在配置文件的路由处，要指定这个filter
 *
 * @author zoutao.blog.csdn.net
 * @date 2021/5/18
 */
//@Configuration
//public class SwaggerControllerFilter extends AbstractGatewayFilterFactory {
//
//    private static final String HEADER_NAME = "X-Forwarded-Prefix";
//    private static final String URI = "/v2/api-docs";
//
//    @Override
//    public GatewayFilter apply(Object config) {
//        return (exchange, chain) -> {
//            ServerHttpRequest request = exchange.getRequest();
//            String path = request.getURI().getPath();
//            if (!StringUtils.endsWithIgnoreCase(path,URI )) {
//                return chain.filter(exchange);
//            }
//            String basePath = path.substring(0, path.lastIndexOf(URI));
//            ServerHttpRequest newRequest = request.mutate().header(HEADER_NAME, basePath).build();
//            ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
//            return chain.filter(newExchange);
//        };
//    }
//}
